<?php

namespace My\Model;

use Utuo\UtuoModel;

class CategoryModel extends UtuoModel {
	
	protected $adminField = 'customer';
	
	// 栏目类型
	public $types = array(
			'news' => '单页图文栏目',
			'list' => '内容列表栏目',
			'url' => '外部链接栏目'
	);
	
	/**
	 * 获取分类详细信息
	 * @param  milit   $id 分类ID或标识
	 * @param  boolean $field 查询字段
	 * @return array     分类信息
	 */
	public function info($id, $field = true) {
		$map = array();
		if(is_numeric($id)){ //通过ID查询
			$map['id'] = $id;
		} else { //通过标识查询
			$map['name'] = $id;
		}
		return $this->field($field)->where($map)->find();
	}
	
	/**
	 * 获取分类树，指定分类则返回指定分类极其子分类，不指定则返回所有分类树
	 * @param  integer $id    分类ID
	 * @param  boolean $field 查询字段
	 * @return array          分类树
	 */
	public function getTree($id = 0, $cid = 0, $field = true) {
		if($id){
			$info = $this->info($id);
			$id   = $info['id'];
		}
		$map  = array('customer' => $cid,
		              'is_deleted' => 0);
		$list = $this->field($field)->where($map)->order('sort')->select();
		$listNew = array();
		$tabTree = \Utuo\TabTree::newInstance();
		$tabTree->listToTree($list, $pk = 'id', $pid = 'pid', $child = '_', $root = $id, $listNew);
		$listNew = empty($listNew)?$listNew:$listNew['_'];
		if(isset($info)){ //指定分类则返回当前分类极其子分类
			$info['_'] = $listNew;
		} else { //否则返回所有分类
			$info = $listNew;
		}
		return $info;
	}
	
	/**
	 * 获得树列表
	 * @param unknown $tree
	 * @return Ambigous <multitype:, array, unknown>
	 */
	public function getTreeList($tree) {
		$tabTree = \Utuo\TabTree::newInstance();
		return $tabTree->treeToList($tree, $child = '_', $order='id');
	}
	
}